package indi.woniu.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import indi.woniu.entity.Paper;
import indi.woniu.entity.ResponseEntity;
import indi.woniu.service.TestPaperService;
import indi.woniu.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author venture
 */
@CrossOrigin
@RestController
@RequestMapping("/testPaper")
public class TestPaperController {
    @Autowired
    private TestPaperService testPaperService;
    @RequestMapping("/findAll")
    public ResponseEntity findAllTestPaper(){
        List<Paper> paperList = testPaperService.findAllTestPaper();
        return new ResponseEntity(200,"成功",paperList);
    }
    @RequestMapping(value = "/addTestPaper",produces = "application/json;charset=UTF-8")
    public ResponseEntity addTestPaper(@RequestBody  Paper paper){
//        System.out.println(aaa);
//
//        Paper paper = null;
//        try {
//            paper = Util.toObject(aaa, Paper.class);
//            System.out.println(paper);
//            Map<String, Object> stringObjectMap = Util.toMap(paper.getSubject());
//
//            paper.setSubjects(stringObjectMap.toString());
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }

        int i  = testPaperService.add(paper);
        if(i >0){
            return new ResponseEntity(200,"成功","添加成功");
        }else{
            return new ResponseEntity(400,"失败","添加失败");
        }
    }
    @PostMapping("/deleteById/{id}")
    //@RequestMapping("/deleteById/{id}")
    public ResponseEntity delete(@PathVariable Integer id){
        int i = testPaperService.deleteById(id);
        if(i >0){
            return new ResponseEntity(200,"成功","删除成功");
        }else{
            return new ResponseEntity(400,"失败","删除失败");
        }

    }
    @RequestMapping("/updateById")
    public ResponseEntity update(@RequestBody Paper paper){
        int i = testPaperService.updateById(paper);
        if(i >0){
            return new ResponseEntity(200,"成功","修改成功");
        }else{
            return new ResponseEntity(400,"失败","修改失败");
        }
    }
    @PostMapping("/findById/{id}")
    public ResponseEntity findById(@PathVariable Integer id){
        Map<String,Object> map = testPaperService.findById(id);

        if(map != null){
            return new ResponseEntity(200,"成功",map);
        }else{
            return new ResponseEntity(400,"失败","查询失败");
        }

    }

}
